package com.iwhalecloud.citybrain.flow.platform.manager.common.client;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import lombok.Data;

/**
 * @author lilh
 * @date 2019-02-18 17:13
 */
@Data
public class HttpCommandResultDto implements Serializable {
    private static final long serialVersionUID = 762572418876450402L;

    private Boolean dealFlag = true;
    private String dealMsg;
    private String processInstanceId;
    private Map<String, String> flowPassList;
    private Map<String, String> flowParamList;
    private int totalCount;
    private Object datas;
    private int pageNum;
    private int pageSize;


    @SuppressWarnings("unchecked")
    public void init(Map<String, Object> paramsMap) {
        this.processInstanceId = String.valueOf(paramsMap.get("PROCESSINSTANCEID"));
        String dealFlag = "0";
        String dealFlagStr = "DEAL_FLAG";
        if (dealFlag.equals(paramsMap.get(dealFlagStr))) {
            this.dealFlag = true;
        } else {
            this.dealFlag = false;
        }

        this.dealMsg = String.valueOf(paramsMap.get("DEAL_MSG"));
        String flowParamListStr = "FLOW_PARAM_LIST";
        if (Objects.nonNull(paramsMap.get(flowParamListStr))) {
            this.flowParamList = (Map) paramsMap.get("FLOW_PARAM_LIST");
        }

        String flowPassListStr = "FLOW_PASS_LIST";
        if (Objects.nonNull(paramsMap.get(flowPassListStr))) {
            this.flowPassList = (Map) paramsMap.get("FLOW_PASS_LIST");
        }

    }

    public Map<String, Object> toMap() {
        Map<String, Object> map = new HashMap<>(4);
        map.put("PROCESSINSTANCEID", this.processInstanceId);
        if (this.dealFlag) {
            map.put("DEAL_FLAG", "0");
        } else {
            map.put("DEAL_FLAG", "1");
        }

        map.put("DEAL_MSG", this.dealMsg);
        if (Objects.nonNull(flowParamList)) {
            map.put("FLOW_PARAM_LIST", this.flowParamList);
        }

        if (Objects.nonNull(this.flowPassList)) {
            map.put("FLOW_PASS_LIST", this.flowPassList);
        }
        return map;
    }

    public static HttpCommandResultDto error(String errorMsg) {
        HttpCommandResultDto result = new HttpCommandResultDto();
        result.setDealFlag(false);
        result.setDealMsg(errorMsg);
        return result;
    }

}
